package me.itsoo.artemis.framework.repository.mapper.resource;

import me.itsoo.artemis.framework.core.tuple.Pair;
import me.itsoo.artemis.framework.core.util.AnnotationUtils;
import me.itsoo.artemis.framework.core.util.CollectionUtils;
import me.itsoo.artemis.framework.core.util.ReflectUtils;
import me.itsoo.artemis.framework.repository.lang.PrimaryKey;
import me.itsoo.artemis.framework.repository.mapper.MapperDefinition;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * ResourceSimpleFactory
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/18
 */
public class ResourceSimpleFactory {

    /**
     * 构建标准 CreateResource
     *
     * @param md   MapperDefinition
     * @param cond T
     * @return CreateResource
     */
    public <T> CreateResource createCreateResource(MapperDefinition md, T cond) {
        if (Objects.isNull(md)) {
            return null;
        }

        final List<Pair<String, Field>> fs = md.getFieldNames();
        if (CollectionUtils.isNotEmpty(fs)) {
            final CreateResource result = CreateResource.insert(md.getTableName());
            for (Pair<String, Field> pair : fs) {
                // 过滤掉主键为 autoIncrement
                final PrimaryKey pkAnn = AnnotationUtils.findAnnotation(pair.getValue(), PrimaryKey.class);
                if (Objects.isNull(pkAnn) || !pkAnn.autoIncrement()) {
                    result.add(pair.getKey(), ReflectUtils.getFieldValue(pair.getValue(), cond));
                }
            }

            return result;
        }

        return null;
    }

    /**
     * 构建标准 DeleteResource
     *
     * @param md MapperDefinition
     * @return DeleteResource
     */
    public DeleteResource createDeleteResource(MapperDefinition md) {
        if (Objects.isNull(md)) {
            return null;
        }

        return DeleteResource.delete(md.getTableName());
    }

    /**
     * 构建标准 UpdateResource
     *
     * @param md   MapperDefinition
     * @param cond T
     * @return UpdateResource
     */
    public <T> UpdateResource createUpdateResource(MapperDefinition md, T cond) {
        if (Objects.isNull(md)) {
            return null;
        }

        final List<Pair<String, Field>> fs = md.getFieldNames();
        if (CollectionUtils.isNotEmpty(fs)) {
            final Collection<String> pks = CollectionUtils.transform(md.getPrimaryKeys(), Pair::getKey);
            final UpdateResource result = UpdateResource.update(md.getTableName());

            for (Pair<String, Field> pair : fs) {
                if (!pks.contains(pair.getKey())) {
                    result.set(pair.getKey(), ReflectUtils.getFieldValue(pair.getValue(), cond));
                }
            }

            return result;
        }

        return null;
    }

    /**
     * 构建标准 QueryResource
     *
     * @param md MapperDefinition
     * @return QueryResource
     */
    public QueryResource createQueryResource(MapperDefinition md) {
        if (Objects.isNull(md)) {
            return null;
        }

        final List<Pair<String, Field>> fs = md.getFieldNames();
        if (CollectionUtils.isNotEmpty(fs)) {
            final QueryResource result = QueryResource.select(md.getTableName());
            for (Pair<String, Field> pair : fs) {
                result.add(pair.getKey());
            }

            return result;
        }

        return null;
    }
}
